package com.powernode.thymeleaf.controller;

import com.powernode.thymeleaf.bean.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

/**
 * @Author: 曾铖坚
 * @Date: 2024/10/15 20:04
 * @Version: 1.0.0
 */
@Controller
public class VarController {
    @RequestMapping(value = "/simvar")
    public String simvar(Model model) {
        model.addAttribute("myname", "张三");
        model.addAttribute("myage", 23);
        return "simvar";
    }

    @RequestMapping(value = "/clavar")
    public String clavar(Model model) {
        Student student = new Student();
        student.setId(1L);
        student.setName("张三");
        student.setEmail("zhangsan@qq.com");
        student.setAge(23);
        student.sex = '男';
        model.addAttribute("stu", student);
        return "clavar";
    }

    @RequestMapping(value = "/address/{id}")
    public String address(@PathVariable("id") String id) {
        System.out.println(id);
        return "ok";
    }

    @RequestMapping(value = "/each")
    public String each(Model model) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(001L, "张三", "zhangsan@qq.com", 23));
        list.add(new Student(002L, "李四", "lisi@qq.com", 24));
        list.add(new Student(003L, "王五", "wangwu@qq.com", 25));
        model.addAttribute("stu", list);
        return "each";
    }

    @RequestMapping(value = "/array")
    public String array(Model model){
        String[] names = new String[]{"张三","李四","王五"};
        model.addAttribute("names",names);
        return "array";
    }

    @RequestMapping(value = "select")
    public String select(Model model){
        // 给表单准备数据
        Map<String,String> citys = new HashMap<>();
        citys.put("001","北京");
        citys.put("002","上海");
        citys.put("003","广州");
        citys.put("004","深圳");
        model.addAttribute("citys",citys);
        model.addAttribute("choice","北京");
        return "select";
    }

    @GetMapping(value = "/insert")
    public String insert(Model model){
        return "insert";
    }

    @GetMapping(value = "/inline")
    public String inline(Model model){
        List<Student> list = new ArrayList<>();
        Student stu1 = new Student(1001L,"张三","zhangsan@qq.com",23);
        Student stu2 = new Student(1002L,"李四","lisi@qq.com",24);
        Student stu3 = new Student(1003L,"王五","wangwu@qq.com",25);
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);

        model.addAttribute("stulist",list);
        model.addAttribute("name","三国演义");
        model.addAttribute("info","我爱<b>中国</b>");
        return "inline";
    }

    @GetMapping(value = "/var")
    public String var(Model model){
        model.addAttribute("age",20);
        return "var";
    }

    @GetMapping(value = "/toolsobject")
    public String toolsObject(Model model){
        model.addAttribute("myname","zhangsan");
        model.addAttribute("mydate",new Date());
        model.addAttribute("price",89.53);
        return "toolsobject";
    }
}
